package com.mvc4g.example.shareModel.client.view.views;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.example.shareModel.client.bean.BeanA;
import com.mvc4g.example.shareModel.client.util.HasModelLoadHandler;
import com.mvc4g.example.shareModel.client.util.ModelLoadEvent;
import com.mvc4g.example.shareModel.client.util.ModelLoadHandler;

public class ViewA extends AbstractView implements HasModelLoadHandler<BeanA> {
	
	HandlerManager handlerManager = new HandlerManager(this);		

	private Label label = null;
	
	private BeanA modelA = null;

	@Override
	protected Widget createWidget() {
		label = new Label();
		return label;
	}
	
	public void setModelA(BeanA modelA){
		this.modelA = modelA;	
		ModelLoadEvent<BeanA> e = new ModelLoadEvent<BeanA>(modelA);
		handlerManager.fireEvent(e);
	}
	
	public BeanA getModel(){
		return modelA;
	}
	
	public void init(){
		label.setText("Information of A: " + modelA.getInfo());
	}
	
	public void restart(){
		modelA = null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public HandlerRegistration addModelLoadHandler(ModelLoadHandler handler) {
		return handlerManager.addHandler(ModelLoadEvent.TYPE, handler);
	}

}
